﻿using NerdDinnerMVC.NerdDinnerService;


namespace NerdDinnerMVC.Models
{
    /// <summary>
    /// Application logic (ui independent, isolation for WCF proxy)
    /// </summary>
    public class DinnerBusinessObject
    {
        public PaginatedDinnerResultList FindUpcomingDinners(int page)
        {
            using (var dinnerServiceClient = new DinnerServiceClient())
            {
                PaginatedDinnerResultList dinners = dinnerServiceClient.FindUpcomingDinners(page, 10);
                return dinners;
            }
        }

        public DinnerViewDTO[] FindByLocation(float longitude, float latitude)
        {
            using (var dinnerServiceClient = new DinnerServiceClient())
            {
                DinnerViewDTO[] dinners = dinnerServiceClient.FindByLocation(longitude, latitude);
                return dinners;
            }
        }

        public void RegisterForDinner(int id , string userName)
        {
            using (var dinnerServiceClient = new DinnerServiceClient())
            {
                dinnerServiceClient.RegisterForDinner(id, userName);
            }

        }

        public DinnerDTO GetDinner(int id)
        {
            using (var dinnerServiceClient = new DinnerServiceClient())
            {
                var dinner = dinnerServiceClient.GetDinner(id);
                return dinner;
            }
        }

        public void UpdateDinner(DinnerDTO dinner)
        {
            using (var dinnerServiceClient = new DinnerServiceClient())
            {
               dinnerServiceClient.UpdateDinner(dinner);
            }
        }

        public int CreateDinner(DinnerDTO dinner)
        {
            using (var dinnerServiceClient = new DinnerServiceClient())
            {
                return dinnerServiceClient.CreateDinner(dinner);
            }
        }

        public void DeleteDinner(int id)
        {
            using (var dinnerServiceClient = new DinnerServiceClient())
            {
                dinnerServiceClient.Delete(id);
            }
        }

        public bool IsAllowedToModifiedBy(DinnerDTO dinner , string userName)
        {
            return (dinner.IsHostedBy(userName));
        }

        public bool IsAllowedToBeDeletedBy(DinnerDTO dinner, string userName)
        {
            return (dinner.IsHostedBy(userName));
        }

    }
}